package top.healthylife.basiss.swagger.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author maoxiaomeng
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    public SwaggerProperties swaggerProperties() {
        return new SwaggerProperties();
    }


    /**
     * 指定扫描包的路径来指定要创建API的目录，一般是控制器这个包
     */
    @Bean
    @ConditionalOnMissingBean(Docket.class)
    public Docket createRestApi(SwaggerProperties swaggerProperties) {
        //Predicate<String> not = Predicates.not(PathSelectors.regex("/error.*"));
        //Predicate<String> not1 = Predicates.not(PathSelectors.regex("/actuator.*"));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(swaggerProperties))
//                .globalOperationParameters(pars)
                .securitySchemes(new ArrayList<>(apiKey()))
                .securityContexts(new ArrayList<>(securityContexts()))
                .select()
                //错误路径不监控
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                //.paths(PathSelectors.regex("/error.*"))
                //.paths(PathSelectors.regex("/actuator.*"))
                .paths(PathSelectors.any())
                .build();
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    private List<ApiKey> apiKey() {
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
        List<ApiKey> arrayList = new ArrayList<>();
        arrayList.add(apiKey);
        return arrayList;
    }

    /**
     * 设置API的基本信息
     *
     * @param swaggerProperties swagger配置
     * @return ApiInfo
     */
    private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .build();
    }
}
